<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
    <style>
        body {
            background-color: #eee;
        }

        a {
            text-decoration: none;
            color: white;
        }

        .container {
            width: 400px;
            margin: 0 auto;
            margin-top: 100px;
            background-color: #fff;
            box-shadow: 0px 0px 5px #888;
            padding: 30px;
        }

        h1 {
            font-size: 2.5rem;
            text-align: center;
        }

        label {
            display: block;
            font-size: 1.2rem;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            border: none;
            border-bottom: 2px solid #ccc;
            font-size: 1.1rem;
            padding: 10px;
            margin-bottom: 20px;
            transition: border-bottom .3s ease-in-out;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            border-bottom-color: #3498db;
        }

        button[type="submit"] {
            background-color: #3498db;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            font-size: 1.2rem;
            cursor: pointer;
        }

        button[type="submit"]:hover {
            background-color: #2775b5;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>登 &nbsp;&nbsp;录</h1>
        <form>
            <label for="software">企业-软件名:</label>
            <select id="software" name="software">
                <option value="ComA-a1">A公司-软件a1</option>
                <option value="ComA-a2">A公司-软件a2</option>
                <option value="ComA-a3">A公司-软件a3</option>
                <option value="ComB-b1">B公司-软件b1</option>
                <option value="ComB-b2">B公司-软件b2</option>
            </select>
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" placeholder="请输入您的用户名">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" placeholder="请输入您的密码">
            <button type="submit">点我登录</button>
        </form>
    </div>
    <script>
        const form = document.getElementsByTagName('form')[0]
        form.addEventListener('submit', function (e) {
            e.preventDefault()
            const softselect = document.getElementById("software")
            const company_app = softselect.options[softselect.selectedIndex].value.split("-")
            const username = document.getElementById('username').value.trim()
            const password = document.getElementById('password').value.trim()
            if (username === '' || password === '') {
                alert('Please enter your username and password.')
            }
            const mes = {
                company: company_app[0],
                software: company_app[1],
                username,
                password,

            }
            fetch("http://localhost:5050/login", {
                method: "post",
                body: JSON.stringify(mes),
                headers: {
                    "Content-Type": "application/json"
                }
            }).then(res => res.json()).then(res => {
                localStorage.setItem("access_token", JSON.stringify(res.access_token))
                localStorage.setItem("refresh_token", JSON.stringify(res.refresh_token))
                window.location.href = "./index.html"
            })

        })
    </script>
</body>

</html>